package com.wfx.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwt.util.JwtUtils;
import com.jwt.util.RsaUtils;
import com.wfx.entity.*;
import com.wfx.mapper.WxbGoodsBrandMapper;
import com.wfx.mapper.WxbGoodsCopyMapper;
import com.wfx.service.IWxbGoodsCopyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import java.security.PublicKey;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2021-01-23
 */
@Service
public class WxbGoodsCopyServiceImpl extends ServiceImpl<WxbGoodsCopyMapper, WxbGoodsCopy> implements IWxbGoodsCopyService {


    @Autowired
    private WxbGoodsCopyMapper wxbGoodsCopyMapper;

    //条件与分页
    @Override
    public PageResult<WxbGoodsCopy> findAll(PageDTO<WxbGoodsCopy> pageDTO) {
        if (pageDTO == null) {
            return new PageResult<WxbGoodsCopy>(false, "查询失败", null, null, null);
        }
        IPage<WxbGoodsCopy> iPage = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        WxbGoodsCopy data = pageDTO.getData();
        QueryWrapper<WxbGoodsCopy> queryWrapper = new QueryWrapper<>(pageDTO.getData());
        if (!StringUtils.isEmpty(data.getCopyTitle())) {
            queryWrapper.eq("copy_title", data.getCopyTitle());
        }
        if (!StringUtils.isEmpty(data.getTypeId())) {
            queryWrapper.eq("type_id", data.getTypeId());
        }
        IPage<WxbGoodsCopy> pageInfo = wxbGoodsCopyMapper.findGoodCopy(iPage, pageDTO.getData());
        PageResult pageResult = new PageResult<>();
        pageResult.setSuccess(true);
        pageResult.setData(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setMsg("查询成功");
        return pageResult;
    }

    //条件与分页
    @Override
    public PageResult<WxbGoodsCopy> findByPage(PageDTO<WxbGoodsCopy> pageDTO) {
        try {
            if (pageDTO == null) {
                return new PageResult<WxbGoodsCopy>(false, "查询失败", null, null, null);
            }
            IPage<WxbGoodsCopy> iPage = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
            WxbGoodsCopy data = pageDTO.getData();
            String publicKeyPath = ResourceUtils.getFile("classpath:ras_public").getPath();
            PublicKey publicKey = RsaUtils.getPublicKey(publicKeyPath);
            //解析数据
            WxbCustomer wxbCustomer = (WxbCustomer) JwtUtils.getInfoFromToken(data.getCustomerId(), publicKey, WxbCustomer.class);
            data.setCustomerId(wxbCustomer.getCustomerId());
            IPage<WxbGoodsCopy> pageInfo = wxbGoodsCopyMapper.findCopy(iPage, data);
            PageResult pageResult = new PageResult<>();
            pageResult.setSuccess(true);
            pageResult.setData(pageInfo.getRecords());
            pageResult.setTotal(pageInfo.getTotal());
            pageResult.setMsg("查询成功");
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //单个删除
    @Override
    public Result deleteGoodCopy(WxbGoodsCopy wxbGoodsCopy) {
        if (wxbGoodsCopy == null) {
            return new Result(false, "参数异常");
        }
        int i = wxbGoodsCopyMapper.deleteById(wxbGoodsCopy.getCopyId());
        return new Result(i == 1 ? true : false, i == 1 ? "删除成功" : "删除失败");
    }

    //批量删除
    @Override
    public Result delSelect(List<WxbGoodsCopy> wxbGoodsCopyList) {
        if (wxbGoodsCopyList.size() == 0) {
            return new Result(false, "参数异常");
        }
        for (WxbGoodsCopy wxbGoodsCopy : wxbGoodsCopyList) {
            wxbGoodsCopyMapper.deleteById(wxbGoodsCopy.getCopyId());
        }
        return new Result(true, "删除成功");
    }

    //添加与修改
    @Override
    public Result addOrUpdate(WxbGoodsCopy wxbGoodsCopy) {

        if (!StringUtils.isEmpty(wxbGoodsCopy.getCopyId())) {
            int i = wxbGoodsCopyMapper.updateById(wxbGoodsCopy);
            return new Result(i == 1 ? true : false, i == 1 ? "修改成功" : "修改失败");
        } else {
            try {
                String publicKeyPath = ResourceUtils.getFile("classpath:ras_public").getPath();
                PublicKey publicKey = RsaUtils.getPublicKey(publicKeyPath);

                //解析数据
                WxbCustomer wxbCustomer = (WxbCustomer) JwtUtils.getInfoFromToken(wxbGoodsCopy.getCustomerId(), publicKey, WxbCustomer.class);
                wxbGoodsCopy.setCustomerId(wxbCustomer.getCustomerId());
                int i = wxbGoodsCopyMapper.insert(wxbGoodsCopy);
                return new Result(i == 1 ? true : false, i == 1 ? "添加成功" : "添加失败");
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }

}
